package com.qf.conterller;

import com.qf.pojo.User;
import com.qf.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//conterller层处理请求并将数据发送到service层
@WebServlet(name = "RegServlet",urlPatterns = "/reg")
public class RegServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //获取请求中的数据并使用BeanUtils工具封装成User对象
            User user = new User();
            BeanUtils.populate(user,req.getParameterMap());
            //将User对象传到Service层进行业务逻辑处理
            UserServiceImpl userService = new UserServiceImpl();
            userService.reg(user);
            //注册成功将数据存储到Session域并跳转到首页
            req.getSession().setAttribute("msg","注册成功，请登录");
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
            return;
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("errMsg","注册失败");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
